package control;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

	public class PermutariMau<T> {
	    public static void main(String args[]) {
	        PermutariMau<Integer> obj = new PermutariMau<Integer>();
	        Collection<Integer> input = new ArrayList<Integer>();
	        if (args.length==0) return;
        	for (String i : args){
	        input.add(Integer.parseInt(i));
	        }
	        Collection<List<Integer>> output = obj.permute(input);
	        int k = 0;
	        Set<List<Integer>> pnr = null;
	        for (int i = 0; i <= input.size(); i++) {
	            pnr = new HashSet<List<Integer>>();
	            for(List<Integer> integers : output){
	            pnr.add(integers.subList(i, integers.size()));
	            }
	            k = input.size()- i;
	            System.out.println("P("+input.size()+","+k+") :"+ 
	            "Count ("+pnr.size()+") :- "+pnr);
	        }
	    }
	    
	    public PermutariMau(){
	    	
	    }
	    
	    public PermutariMau(ArrayList<Integer> listaCifre, int pas){
	    	intoarcePermutariCuPas(listaCifre, pas);
	    }
	    
	    
		protected HashSet<List<Integer>> intoarcePermutariCuPas(ArrayList<Integer> listaCifre, int pas)
		{
			PermutariMau<Integer> obj = new PermutariMau<Integer>();
	        Collection<Integer> input = new ArrayList<Integer>();
	        if (listaCifre.size()==0) return null;
	        for (int i : listaCifre){
	        input.add(i);
	        }
	        
	        Collection<List<Integer>> output = obj.permute(input);
	        
	        HashSet<List<Integer>> pnr = null;
	        pnr = new HashSet<List<Integer>>();
	        for(List<Integer> integers : output){
	        	pnr.add(integers.subList(pas, integers.size()));
	        }
	        //int k = 0;
	        //k = input.size()- pas;
//	        System.out.println("P("+input.size()+","+k+") :" + "Count ("+pnr.size()+") :- "+pnr);
	        return  pnr;
		}
	    
	    public Collection<List<T>> permute(Collection<T> input) {
	        Collection<List<T>> output = new ArrayList<List<T>>();
	        if (input.isEmpty()) {
	            output.add(new ArrayList<T>());
	            return output;
	        }
	        List<T> list = new ArrayList<T>(input);
	        T head = list.get(0);
	        List<T> rest = list.subList(1, list.size());
	        for (List<T> permutations : permute(rest)) {
	            List<List<T>> subLists = new ArrayList<List<T>>();
	            for (int i = 0; i <= permutations.size(); i++) {
	                List<T> subList = new ArrayList<T>();
	                subList.addAll(permutations);
	                subList.add(i, head);
	                subLists.add(subList);
	            }
	            output.addAll(subLists);
	        }
	        return output;
	    }
	}
